package com.sc.controller;

import cn.hutool.core.bean.BeanUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.sc.dto.DictionariesInsertDTO;
import com.sc.dto.DictionariesPageDTO;
import com.sc.dto.DictionariesUpdateDTO;
import com.sc.entity.Student;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.sc.entity.Dictionaries;
import com.sc.service.DictionariesService;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典表 控制层。
 *
 * @author LiYe
 * @since 2024-09-25
 */
@RestController
@RequestMapping("/api/v1/dictionary")
public class DictionariesController {

    @Autowired
    private DictionariesService dictionariesService;

    /**
     * 高校 - 院系 - 专业 信息三级树
     *
     * @return
     */
    @GetMapping("scmTree")
    public List<HashMap> scmTree() {
        return dictionariesService.getSCMTree();
    }

    /**
     * 添加字典表。
     *
     * @param dto 字典表
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    public boolean save(@RequestBody DictionariesInsertDTO dto) {
        Dictionaries dictionaries = BeanUtil.copyProperties(dto, Dictionaries.class);
        return dictionariesService.save(dictionaries);
    }

    /**
     * 根据主键删除字典表。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return dictionariesService.removeById(id);
    }

    /**
     * 根据主键更新字典表。
     *
     * @param dto 字典表
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    public boolean update(@RequestBody DictionariesUpdateDTO dto) {
        Dictionaries dictionaries = BeanUtil.copyProperties(dto, Dictionaries.class);
        return dictionariesService.updateById(dictionaries, true);
    }

    /**
     * 查询所有字典表。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public List<Dictionaries> list(@RequestParam Integer pid) {
        QueryWrapper queryWrapper = QueryWrapper.create().select("id", "name").eq("pid", pid);

        return dictionariesService.list(queryWrapper);
    }

    /**
     * 根据字典表主键获取详细信息。
     *
     * @param id 字典表主键
     * @return 字典表详情
     */
    @GetMapping("getInfo/{id}")
    public Dictionaries getInfo(@PathVariable Serializable id) {
        return dictionariesService.getById(id);
    }

    /**
     * 分页查询字典表。
     *
     * @param dto 分页对象
     * @return 分页对象
     */
    @PostMapping("page")
    public Page<Dictionaries> page(@RequestBody DictionariesPageDTO dto) {
        Page<Dictionaries> page = Page.of(dto.getPageNum(), dto.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        if (dto.getName() != null) {
            queryWrapper.like("name", dto.getName());
        }
        return dictionariesService.pageAs(page, queryWrapper, Dictionaries.class);
    }

}
